import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterstudyapplast/business/viewmodels/article_collect_viwemodel.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutterstudyapplast/services/service_locator.dart';
import 'package:toast/toast.dart';
import 'package:provider/provider.dart';
import 'package:flutterstudyapplast/ui/page/page_collect_add_website.dart';

class CollectWebsitPage extends StatefulWidget {
  @override
  _CollectWebsitPageState createState() => _CollectWebsitPageState();
}

class _CollectWebsitPageState extends State<CollectWebsitPage> with AutomaticKeepAliveClientMixin{

  ArticleCollectViewModel _articleCollectViewModel = serviceLocator<ArticleCollectViewModel>();

  bool isHidden = false;

  Future<void> _getWebsites() async{
    await _articleCollectViewModel.getWebsites();
    isHidden = true;
    return null;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getWebsites();
  }
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context)=>_articleCollectViewModel,
      child: Consumer<ArticleCollectViewModel>(
        builder: (context,model,child){
          return Stack(
            children: <Widget>[
              Offstage(
                offstage: isHidden,
                child: Center(child: CircularProgressIndicator(),),
              ),
              Offstage(
                offstage: !isHidden || _articleCollectViewModel.websites.isNotEmpty,
                child: Center(child: Text("你还没有收藏内容..."),),
              ),
              Offstage(
                offstage: _articleCollectViewModel.websites.isEmpty,
                child: RefreshIndicator(
                  onRefresh: _getWebsites,
                  child: ListView.separated(
                    padding: EdgeInsets.all(22),
                    physics: AlwaysScrollableScrollPhysics(),
                    itemBuilder: (context,index)=>_buildItem(context,_articleCollectViewModel.websites[index]),
                    separatorBuilder: (context,index){
                      return Padding(
                        padding: EdgeInsets.only(top: 8,bottom: 8),
                        child: Divider(color: Colors.grey,),
                      );
                    },
                    itemCount: _articleCollectViewModel.websites.length,
                  ),
                ),
              ),
              Positioned(
                bottom: 20,
                right: 20,
                child: FloatingActionButton(
                  child: Icon(Icons.add),
                  onPressed: _addCollect,
                ),
              )
            ],
          );
        },
      ),
    );

  }

  Widget _buildItem(BuildContext context,item){
    ///侧滑删除
    return Slidable(
      delegate: SlidableDrawerDelegate(),
      secondaryActions: <Widget>[
        IconSlideAction(
          caption: "删除",
          color: Colors.red,
          icon: Icons.delete,
          onTap: ()=>_delCollect(item),
        )
      ],
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(item.title??"没有标题",style: TextStyle(fontSize: 22),),
          Padding(padding: EdgeInsets.only(top: 8),child: Text(item.url??"暂无地址",style: TextStyle(color: Theme.of(context).primaryColor),),)
        ],
      ),
    );
  }
  _addCollect() async{
    var result = await Navigator.push(
        context, MaterialPageRoute(builder: (_) => CollectAddWebsite()));
    if (result != null) {
      _articleCollectViewModel.addCollectWeb(result);
    }
  }
  _delCollect(item) async{
    showDialog(context: context,barrierDismissible: false,builder: (_){
      return Center(child: CircularProgressIndicator(),);
    });
    var result = await _articleCollectViewModel.unCollectWebsite(item.id);
    Navigator.pop(context);
    if (!result) {
      Toast.show("删除失败", context, gravity: Toast.CENTER);
    } else {
      _articleCollectViewModel.removeCollectWeb(item);
    }
  }
  /// tab切换不重置
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
